package com.mstar.qs.common.io;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Class that is used to serialize an object to string literal and unserialize
 * and string to object
 * 
 * @author dchen2
 * 
 * @param <T>
 */
public class SerializeUtil<T> {

	/**
	 * com.mstar.qs.common.io.SerializeUtil.java
	 * 
	 * serialize an object to string literal.
	 * 
	 * @param ui
	 * @return
	 * @throws IOException
	 */
	public String serialize(T ui) throws IOException {
		return serializeToString(ui);
	}
	
	public static <T> String serializeToString(T ui) throws IOException {
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(
				byteArrayOutputStream);
		objectOutputStream.writeObject(ui);
		String serStr = byteArrayOutputStream.toString("ISO-8859-1");
		serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
		objectOutputStream.close();
		byteArrayOutputStream.close();
		return serStr;
	}

	/**
	 * Unserialize string literal to object
	 * 
	 * @param sui
	 *            String literal just serialized using <url>serialize</url>
	 * @return
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public static <T> T unserializeFrom(String sui) throws IOException, ClassNotFoundException {
		String redStr = java.net.URLDecoder.decode(sui, "UTF-8");
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
				redStr.getBytes("ISO-8859-1"));
		ObjectInputStream objectInputStream = new ObjectInputStream(
				byteArrayInputStream);
		T ui = (T) objectInputStream.readObject();
		objectInputStream.close();
		byteArrayInputStream.close();
		return ui;
	}
	
	public T unserialize(String sui) throws IOException, ClassNotFoundException {
		return unserializeFrom(sui);
	}

	/**
	 * Serialize an object to xml string
	 * @param o object that wanted to be converted to
	 * @return 
	 */
	public static <T> String encode(T o) {

		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		XMLEncoder e = new XMLEncoder(new BufferedOutputStream(
				byteArrayOutputStream));
		e.writeObject(o);
		e.close();
		return byteArrayOutputStream.toString();

	}
	public String encodeToXml(T o) {
		return encode(o);

	}

	/**
	 * Unserialize xml string to associated object
	 * 
	 * @param s xml string that converted using method  encodeToXml(T o)
	 * @return
	 */
	public static <T> T decode(String s) {
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
				s.getBytes());
		XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
				byteArrayInputStream));
		T o = (T) decoder.readObject();
		decoder.close();
		return o;

	}
	public T decodeFromXml(String s) {
		return decode(s);
	}

}
